package jjn.round1;

/**
 * @author Jjn
 * @since 2022/10/4 22:58
 */
public class LeetCode921_MinAddToMakeValidV2 {

    public int minAddToMakeValid(String s) {
        int total = 0;
        int needRight = 0;
        for (char c : s.toCharArray()) {
            if (c == '(') {
                needRight++;
            } else if (c == ')' && needRight > 0) {
                needRight--;
            } else {
                total++;
            }
        }
        total += needRight;
        return total;
    }
}
